<?php

namespace app\user\controller\mall\oper;

use app\user\controller\mall\Base;
use think\Db;

/**
 * 举报
 *
 * @icon fa fa-circle-o
 */
class Inform extends Base
{
    
    /**
     * Informs模型对象
     * @var \app\common\model\mall\oper\Inform
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\mall\oper\Inform;
        $this->view->assign("stateList", $this->model->getStateList());
        $this->view->assign("informTypeList", $this->model->getInformTypeList());
    }

    /**
     * 添加举报
     */
    public function add(){
        $goods_id = input('goods_id');
        //判断用户是否拥有举报权利
        $s = Db::name('mall_user_extra')->where("user_id",$this->auth->id)->find();
        if($s['informis']==0) $this->error("你已被禁止举报！");
        //判断举报对象（商品）是否存在
        $row = Db::name('mall_goods_goods')->alias("g")
            ->join("__MALL_SHOP_SHOP__ s","g.shop_id=s.id and s.state=1 and s.status='normal'","LEFT")
            ->field("g.*,s.name shopName")
            ->where(['g.goodsstatus'=>1,'g.status'=>["in",["sale","warn"]],'g.id'=>$goods_id])->find();
        if(empty($row)) $this->error("举报失败，无效的举报对象");

        if ($this->request->isPost()){
            $params = $this->request->post("row/a");
            if ($params){
                try{
                    $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model)) . '.add';
                    $result = $this->validate($params,$validate);
                    if(true !== $result){
                        // 验证失败 输出错误信息
                        $this->error($result);
                    }
                    $params['user_id'] = $this->auth->id;
                    $result = $this->model->allowField(true)->save($params);
                    if ($result !== false){
                        $this->success();
                    }else{
                        $this->error($this->model->getError());
                    }
                }
                catch (\think\exception\PDOException $e)
                {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->assign("row",$row);
        return $this->fetch();
    }

    /**
     * 添加举报
     */
    public function detail($ids = null){
        //判断举报对象（商品）是否存在
        $row = $this->model->alias("i")
            ->join('__MALL_GOODS_GOODS__ g',"i.goods_id=g.id","LEFT")
            ->join("__MALL_SHOP_SHOP__ s","i.shop_id=s.id and s.state=1 and s.status='normal'","LEFT")
            ->field("i.*,g.goodsname,g.goodsimg,s.name shopName")
            ->where(['i.id'=>["in",$ids]])->find();
        if(empty($row)) $this->error("记录不存在");
        $this->assign("row",$row);
        return $this->fetch();
    }
}
